package com.hd.trans.db.greenDao;

import java.util.List;
import java.util.ArrayList;
import android.database.Cursor;
import android.database.sqlite.SQLiteStatement;

import org.greenrobot.greendao.AbstractDao;
import org.greenrobot.greendao.Property;
import org.greenrobot.greendao.internal.SqlUtils;
import org.greenrobot.greendao.internal.DaoConfig;
import org.greenrobot.greendao.database.Database;
import org.greenrobot.greendao.database.DatabaseStatement;

import com.hd.trans.db.bean.HuDunLanguage;

import com.hd.trans.db.bean.FileTranslateRecord;

// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
/** 
 * DAO for table "FILE_TRANSLATE_RECORD".
*/
public class FileTranslateRecordDao extends AbstractDao<FileTranslateRecord, Long> {

    public static final String TABLENAME = "FILE_TRANSLATE_RECORD";

    /**
     * Properties of entity FileTranslateRecord.<br/>
     * Can be used for QueryBuilder and for referencing column names.
     */
    public static class Properties {
        public final static Property Id = new Property(0, Long.class, "id", true, "_id");
        public final static Property TextLanguageFromId = new Property(1, Long.class, "textLanguageFromId", false, "TEXT_LANGUAGE_FROM_ID");
        public final static Property TextLanguageToId = new Property(2, Long.class, "textLanguageToId", false, "TEXT_LANGUAGE_TO_ID");
        public final static Property FileLanguageFromId = new Property(3, Long.class, "fileLanguageFromId", false, "FILE_LANGUAGE_FROM_ID");
        public final static Property FileLanguageToId = new Property(4, Long.class, "fileLanguageToId", false, "FILE_LANGUAGE_TO_ID");
        public final static Property CreateTime = new Property(5, long.class, "createTime", false, "create_time");
        public final static Property UpdateTime = new Property(6, long.class, "updateTime", false, "update_time");
        public final static Property OriginalText = new Property(7, String.class, "originalText", false, "original_text");
        public final static Property TranslateText = new Property(8, String.class, "translateText", false, "translate_text");
        public final static Property OriginalFilePath = new Property(9, String.class, "originalFilePath", false, "original_file_path");
        public final static Property TranslateFilePath = new Property(10, String.class, "translateFilePath", false, "translate_file_path");
        public final static Property MetaFilePath = new Property(11, String.class, "metaFilePath", false, "meta_file_path");
        public final static Property InpaintedFilePath = new Property(12, String.class, "inpaintedFilePath", false, "inpainted_file_path");
        public final static Property IsCollected = new Property(13, boolean.class, "isCollected", false, "is_collected");
        public final static Property FileName = new Property(14, String.class, "fileName", false, "file_name");
        public final static Property RecordType = new Property(15, int.class, "recordType", false, "record_type");
        public final static Property TranslateState = new Property(16, int.class, "translateState", false, "record_state");
    }

    private DaoSession daoSession;


    public FileTranslateRecordDao(DaoConfig config) {
        super(config);
    }
    
    public FileTranslateRecordDao(DaoConfig config, DaoSession daoSession) {
        super(config, daoSession);
        this.daoSession = daoSession;
    }

    /** Creates the underlying database table. */
    public static void createTable(Database db, boolean ifNotExists) {
        String constraint = ifNotExists? "IF NOT EXISTS ": "";
        db.execSQL("CREATE TABLE " + constraint + "\"FILE_TRANSLATE_RECORD\" (" + //
                "\"_id\" INTEGER PRIMARY KEY AUTOINCREMENT ," + // 0: id
                "\"TEXT_LANGUAGE_FROM_ID\" INTEGER," + // 1: textLanguageFromId
                "\"TEXT_LANGUAGE_TO_ID\" INTEGER," + // 2: textLanguageToId
                "\"FILE_LANGUAGE_FROM_ID\" INTEGER," + // 3: fileLanguageFromId
                "\"FILE_LANGUAGE_TO_ID\" INTEGER," + // 4: fileLanguageToId
                "\"create_time\" INTEGER NOT NULL ," + // 5: createTime
                "\"update_time\" INTEGER NOT NULL ," + // 6: updateTime
                "\"original_text\" TEXT," + // 7: originalText
                "\"translate_text\" TEXT," + // 8: translateText
                "\"original_file_path\" TEXT," + // 9: originalFilePath
                "\"translate_file_path\" TEXT," + // 10: translateFilePath
                "\"meta_file_path\" TEXT," + // 11: metaFilePath
                "\"inpainted_file_path\" TEXT," + // 12: inpaintedFilePath
                "\"is_collected\" INTEGER NOT NULL ," + // 13: isCollected
                "\"file_name\" TEXT," + // 14: fileName
                "\"record_type\" INTEGER NOT NULL ," + // 15: recordType
                "\"record_state\" INTEGER NOT NULL );"); // 16: translateState
    }

    /** Drops the underlying database table. */
    public static void dropTable(Database db, boolean ifExists) {
        String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "\"FILE_TRANSLATE_RECORD\"";
        db.execSQL(sql);
    }

    @Override
    protected final void bindValues(DatabaseStatement stmt, FileTranslateRecord entity) {
        stmt.clearBindings();
 
        Long id = entity.getId();
        if (id != null) {
            stmt.bindLong(1, id);
        }
 
        Long textLanguageFromId = entity.getTextLanguageFromId();
        if (textLanguageFromId != null) {
            stmt.bindLong(2, textLanguageFromId);
        }
 
        Long textLanguageToId = entity.getTextLanguageToId();
        if (textLanguageToId != null) {
            stmt.bindLong(3, textLanguageToId);
        }
 
        Long fileLanguageFromId = entity.getFileLanguageFromId();
        if (fileLanguageFromId != null) {
            stmt.bindLong(4, fileLanguageFromId);
        }
 
        Long fileLanguageToId = entity.getFileLanguageToId();
        if (fileLanguageToId != null) {
            stmt.bindLong(5, fileLanguageToId);
        }
        stmt.bindLong(6, entity.getCreateTime());
        stmt.bindLong(7, entity.getUpdateTime());
 
        String originalText = entity.getOriginalText();
        if (originalText != null) {
            stmt.bindString(8, originalText);
        }
 
        String translateText = entity.getTranslateText();
        if (translateText != null) {
            stmt.bindString(9, translateText);
        }
 
        String originalFilePath = entity.getOriginalFilePath();
        if (originalFilePath != null) {
            stmt.bindString(10, originalFilePath);
        }
 
        String translateFilePath = entity.getTranslateFilePath();
        if (translateFilePath != null) {
            stmt.bindString(11, translateFilePath);
        }
 
        String metaFilePath = entity.getMetaFilePath();
        if (metaFilePath != null) {
            stmt.bindString(12, metaFilePath);
        }
 
        String inpaintedFilePath = entity.getInpaintedFilePath();
        if (inpaintedFilePath != null) {
            stmt.bindString(13, inpaintedFilePath);
        }
        stmt.bindLong(14, entity.getIsCollected() ? 1L: 0L);
 
        String fileName = entity.getFileName();
        if (fileName != null) {
            stmt.bindString(15, fileName);
        }
        stmt.bindLong(16, entity.getRecordType());
        stmt.bindLong(17, entity.getTranslateState());
    }

    @Override
    protected final void bindValues(SQLiteStatement stmt, FileTranslateRecord entity) {
        stmt.clearBindings();
 
        Long id = entity.getId();
        if (id != null) {
            stmt.bindLong(1, id);
        }
 
        Long textLanguageFromId = entity.getTextLanguageFromId();
        if (textLanguageFromId != null) {
            stmt.bindLong(2, textLanguageFromId);
        }
 
        Long textLanguageToId = entity.getTextLanguageToId();
        if (textLanguageToId != null) {
            stmt.bindLong(3, textLanguageToId);
        }
 
        Long fileLanguageFromId = entity.getFileLanguageFromId();
        if (fileLanguageFromId != null) {
            stmt.bindLong(4, fileLanguageFromId);
        }
 
        Long fileLanguageToId = entity.getFileLanguageToId();
        if (fileLanguageToId != null) {
            stmt.bindLong(5, fileLanguageToId);
        }
        stmt.bindLong(6, entity.getCreateTime());
        stmt.bindLong(7, entity.getUpdateTime());
 
        String originalText = entity.getOriginalText();
        if (originalText != null) {
            stmt.bindString(8, originalText);
        }
 
        String translateText = entity.getTranslateText();
        if (translateText != null) {
            stmt.bindString(9, translateText);
        }
 
        String originalFilePath = entity.getOriginalFilePath();
        if (originalFilePath != null) {
            stmt.bindString(10, originalFilePath);
        }
 
        String translateFilePath = entity.getTranslateFilePath();
        if (translateFilePath != null) {
            stmt.bindString(11, translateFilePath);
        }
 
        String metaFilePath = entity.getMetaFilePath();
        if (metaFilePath != null) {
            stmt.bindString(12, metaFilePath);
        }
 
        String inpaintedFilePath = entity.getInpaintedFilePath();
        if (inpaintedFilePath != null) {
            stmt.bindString(13, inpaintedFilePath);
        }
        stmt.bindLong(14, entity.getIsCollected() ? 1L: 0L);
 
        String fileName = entity.getFileName();
        if (fileName != null) {
            stmt.bindString(15, fileName);
        }
        stmt.bindLong(16, entity.getRecordType());
        stmt.bindLong(17, entity.getTranslateState());
    }

    @Override
    protected final void attachEntity(FileTranslateRecord entity) {
        super.attachEntity(entity);
        entity.__setDaoSession(daoSession);
    }

    @Override
    public Long readKey(Cursor cursor, int offset) {
        return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0);
    }    

    @Override
    public FileTranslateRecord readEntity(Cursor cursor, int offset) {
        FileTranslateRecord entity = new FileTranslateRecord( //
            cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // id
            cursor.isNull(offset + 1) ? null : cursor.getLong(offset + 1), // textLanguageFromId
            cursor.isNull(offset + 2) ? null : cursor.getLong(offset + 2), // textLanguageToId
            cursor.isNull(offset + 3) ? null : cursor.getLong(offset + 3), // fileLanguageFromId
            cursor.isNull(offset + 4) ? null : cursor.getLong(offset + 4), // fileLanguageToId
            cursor.getLong(offset + 5), // createTime
            cursor.getLong(offset + 6), // updateTime
            cursor.isNull(offset + 7) ? null : cursor.getString(offset + 7), // originalText
            cursor.isNull(offset + 8) ? null : cursor.getString(offset + 8), // translateText
            cursor.isNull(offset + 9) ? null : cursor.getString(offset + 9), // originalFilePath
            cursor.isNull(offset + 10) ? null : cursor.getString(offset + 10), // translateFilePath
            cursor.isNull(offset + 11) ? null : cursor.getString(offset + 11), // metaFilePath
            cursor.isNull(offset + 12) ? null : cursor.getString(offset + 12), // inpaintedFilePath
            cursor.getShort(offset + 13) != 0, // isCollected
            cursor.isNull(offset + 14) ? null : cursor.getString(offset + 14), // fileName
            cursor.getInt(offset + 15), // recordType
            cursor.getInt(offset + 16) // translateState
        );
        return entity;
    }
     
    @Override
    public void readEntity(Cursor cursor, FileTranslateRecord entity, int offset) {
        entity.setId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0));
        entity.setTextLanguageFromId(cursor.isNull(offset + 1) ? null : cursor.getLong(offset + 1));
        entity.setTextLanguageToId(cursor.isNull(offset + 2) ? null : cursor.getLong(offset + 2));
        entity.setFileLanguageFromId(cursor.isNull(offset + 3) ? null : cursor.getLong(offset + 3));
        entity.setFileLanguageToId(cursor.isNull(offset + 4) ? null : cursor.getLong(offset + 4));
        entity.setCreateTime(cursor.getLong(offset + 5));
        entity.setUpdateTime(cursor.getLong(offset + 6));
        entity.setOriginalText(cursor.isNull(offset + 7) ? null : cursor.getString(offset + 7));
        entity.setTranslateText(cursor.isNull(offset + 8) ? null : cursor.getString(offset + 8));
        entity.setOriginalFilePath(cursor.isNull(offset + 9) ? null : cursor.getString(offset + 9));
        entity.setTranslateFilePath(cursor.isNull(offset + 10) ? null : cursor.getString(offset + 10));
        entity.setMetaFilePath(cursor.isNull(offset + 11) ? null : cursor.getString(offset + 11));
        entity.setInpaintedFilePath(cursor.isNull(offset + 12) ? null : cursor.getString(offset + 12));
        entity.setIsCollected(cursor.getShort(offset + 13) != 0);
        entity.setFileName(cursor.isNull(offset + 14) ? null : cursor.getString(offset + 14));
        entity.setRecordType(cursor.getInt(offset + 15));
        entity.setTranslateState(cursor.getInt(offset + 16));
     }
    
    @Override
    protected final Long updateKeyAfterInsert(FileTranslateRecord entity, long rowId) {
        entity.setId(rowId);
        return rowId;
    }
    
    @Override
    public Long getKey(FileTranslateRecord entity) {
        if(entity != null) {
            return entity.getId();
        } else {
            return null;
        }
    }

    @Override
    public boolean hasKey(FileTranslateRecord entity) {
        return entity.getId() != null;
    }

    @Override
    protected final boolean isEntityUpdateable() {
        return true;
    }
    
    private String selectDeep;

    protected String getSelectDeep() {
        if (selectDeep == null) {
            StringBuilder builder = new StringBuilder("SELECT ");
            SqlUtils.appendColumns(builder, "T", getAllColumns());
            builder.append(',');
            SqlUtils.appendColumns(builder, "T0", daoSession.getHuDunLanguageDao().getAllColumns());
            builder.append(',');
            SqlUtils.appendColumns(builder, "T1", daoSession.getHuDunLanguageDao().getAllColumns());
            builder.append(',');
            SqlUtils.appendColumns(builder, "T2", daoSession.getHuDunLanguageDao().getAllColumns());
            builder.append(',');
            SqlUtils.appendColumns(builder, "T3", daoSession.getHuDunLanguageDao().getAllColumns());
            builder.append(" FROM FILE_TRANSLATE_RECORD T");
            builder.append(" LEFT JOIN HU_DUN_LANGUAGE T0 ON T.\"TEXT_LANGUAGE_FROM_ID\"=T0.\"_id\"");
            builder.append(" LEFT JOIN HU_DUN_LANGUAGE T1 ON T.\"TEXT_LANGUAGE_TO_ID\"=T1.\"_id\"");
            builder.append(" LEFT JOIN HU_DUN_LANGUAGE T2 ON T.\"FILE_LANGUAGE_FROM_ID\"=T2.\"_id\"");
            builder.append(" LEFT JOIN HU_DUN_LANGUAGE T3 ON T.\"FILE_LANGUAGE_TO_ID\"=T3.\"_id\"");
            builder.append(' ');
            selectDeep = builder.toString();
        }
        return selectDeep;
    }
    
    protected FileTranslateRecord loadCurrentDeep(Cursor cursor, boolean lock) {
        FileTranslateRecord entity = loadCurrent(cursor, 0, lock);
        int offset = getAllColumns().length;

        HuDunLanguage textLanguageFrom = loadCurrentOther(daoSession.getHuDunLanguageDao(), cursor, offset);
        entity.setTextLanguageFrom(textLanguageFrom);
        offset += daoSession.getHuDunLanguageDao().getAllColumns().length;

        HuDunLanguage textLanguageTo = loadCurrentOther(daoSession.getHuDunLanguageDao(), cursor, offset);
        entity.setTextLanguageTo(textLanguageTo);
        offset += daoSession.getHuDunLanguageDao().getAllColumns().length;

        HuDunLanguage fileLanguageFrom = loadCurrentOther(daoSession.getHuDunLanguageDao(), cursor, offset);
        entity.setFileLanguageFrom(fileLanguageFrom);
        offset += daoSession.getHuDunLanguageDao().getAllColumns().length;

        HuDunLanguage fileLanguageTo = loadCurrentOther(daoSession.getHuDunLanguageDao(), cursor, offset);
        entity.setFileLanguageTo(fileLanguageTo);

        return entity;    
    }

    public FileTranslateRecord loadDeep(Long key) {
        assertSinglePk();
        if (key == null) {
            return null;
        }

        StringBuilder builder = new StringBuilder(getSelectDeep());
        builder.append("WHERE ");
        SqlUtils.appendColumnsEqValue(builder, "T", getPkColumns());
        String sql = builder.toString();
        
        String[] keyArray = new String[] { key.toString() };
        Cursor cursor = db.rawQuery(sql, keyArray);
        
        try {
            boolean available = cursor.moveToFirst();
            if (!available) {
                return null;
            } else if (!cursor.isLast()) {
                throw new IllegalStateException("Expected unique result, but count was " + cursor.getCount());
            }
            return loadCurrentDeep(cursor, true);
        } finally {
            cursor.close();
        }
    }
    
    /** Reads all available rows from the given cursor and returns a list of new ImageTO objects. */
    public List<FileTranslateRecord> loadAllDeepFromCursor(Cursor cursor) {
        int count = cursor.getCount();
        List<FileTranslateRecord> list = new ArrayList<FileTranslateRecord>(count);
        
        if (cursor.moveToFirst()) {
            if (identityScope != null) {
                identityScope.lock();
                identityScope.reserveRoom(count);
            }
            try {
                do {
                    list.add(loadCurrentDeep(cursor, false));
                } while (cursor.moveToNext());
            } finally {
                if (identityScope != null) {
                    identityScope.unlock();
                }
            }
        }
        return list;
    }
    
    protected List<FileTranslateRecord> loadDeepAllAndCloseCursor(Cursor cursor) {
        try {
            return loadAllDeepFromCursor(cursor);
        } finally {
            cursor.close();
        }
    }
    

    /** A raw-style query where you can pass any WHERE clause and arguments. */
    public List<FileTranslateRecord> queryDeep(String where, String... selectionArg) {
        Cursor cursor = db.rawQuery(getSelectDeep() + where, selectionArg);
        return loadDeepAllAndCloseCursor(cursor);
    }
 
}
